$(function(){
	
	//获取输入框焦点
	$('input[type="text"],input[type="tel"],input[type="password"],textarea').on('click', function(){
		var target = this;
		setTimeout(function(){
    		target.scrollIntoViewIfNeeded();
    	},400);
	});
	
	//返回上一页
	if($(".back_btn").length>0){
		$(".back_btn").each(function(){
			var target = $(this);
			target.on('click',function(){
				var type = target.attr('data-type');
				if(type==0 || type==null || type==undefined){
					//返回上一页
					history.go(-1);
				}else if(type==1){
					//关闭当前页
					closeSubMain(target.parents('.subMain'));
				};
			});
		});
	};
	
});


function closeSubMain(obj){
	obj.css('transform','translate3d(100%,0,0)');
};

/* 页面通用滑动
 * obj 为swiper-container对象的id,必选
 * down 为启用上拉加载的布尔值，可选
 * up 为启用下拉刷新的布尔值，可选
 * */

function pullSwiper(obj,options){
	
	var options = options == undefined ? options = {} : options = options;
	options.down == undefined ? options.down = false : options.down ;
	options.up == undefined ? options.up = false : options.up ;
	
	var isUp = false;
	var isDown = false;
	var swiper = new Swiper(obj,{
		direction: 'vertical',//vertical：竖向切换,horizontal: 横向切换
	    slidesPerView: 'auto',
	    setWrapperSize :true,
	    mousewheel: true,

	    freeMode:true,
	    passiveListeners : false,
	    
	    preloadImages:false,//默认为true，Swiper会强制加载所有图片。
		updateOnImagesReady : true,//当所有的内嵌图像（img标签）加载完成后Swiper会重新初始化
	    
	    observer:true,//当改变swiper的样式（例如隐藏/显示）或者修改swiper的子元素时，自动初始化swiper。
    	observeParents:true,//当Swiper的父元素变化，自动初始化swiper。
    
	    // 如果需要滚动条
	    //scrollbar: {
	    //  el: obj+' .swiper-scrollbar',
	    //  hide: true,//自动隐藏
	    //},
	    
	   	onInit:function(){
	   		var id = obj.replace(/\#/g,'');
	   		var main = document.getElementById(id);
	   		//var scroll = document.createElement('div');
	   		//scroll.className ='swiper-scrollbar';
	   		//main.append(scroll);
	   		if(options.up){
	   			var pull = document.createElement('div');
	   			pull.className = 'pull-up';
	   			pull.innerText = '下拉刷新';
	   			main.querySelector('.content').prepend(pull);
   			};
   			if(options.down){
	   			var pull = document.createElement('div');
	   			pull.className = 'pull-down';
	   			pull.innerText = '上拉加载';
	   			main.querySelector('.content').append(pull);
   			};
	   	},
	   	onTouchStart:function(event){
	   		swiper.update();
	   		if(options.up){
				var pullUp = swiper.slides.eq(0).find('.pull-up')[0];
				pullUp.innerHTML = '下拉刷新';
   			};
	   	},
		onTouchMove:function(event){
			//获取滚动区域总高度
			var slideH = parseFloat(swiper.slides.eq(0).css('height'));
			//获取滚动容器总高度
   			var containH = swiper.height;
   			//可滚动最大高度
   			var maxH = containH-slideH;
   			
   			//下拉刷新为真
   			if(options.up){
   				if(swiper.translate >= 40){
					var pullUp = swiper.slides.eq(0).find('.pull-up')[0];
					pullUp.innerHTML = '释放刷新';
					isUp = true;
	   			}else{
	   				var pullUp = swiper.slides.eq(0).find('.pull-up')[0];
	   				pullUp.innerHTML = '下拉刷新';
	   				isUp = false;
	   			};
   			};
   			
   			//上拉加载为真
   			if(options.down){
   				var pullDown = swiper.slides.eq(0).find('.pull-down')[0];
   				if(maxH < 0){
   					//pullDown.style.display = 'block';
   					if(swiper.translate <= maxH){
						pullDown.innerHTML = '释放加载';
						isDown = true;
	   				}else{
	   					pullDown.innerHTML = '上拉加载';
	   					isDown = false;
	   				};
   				};
   			};
		},
		onTouchEnd:function(event){
			
			if(isUp){
				var pullUp = swiper.slides.eq(0).find('.pull-up')[0];
   				pullUp.innerHTML = '正在刷新…';
   				swiper.setWrapperTranslate(40);
   				pullUp.style.position = 'relative';
   				pullUp.style.top = '0';
   				setTimeout(function(){
   					if(typeof options.down == 'function'){
						options.up(pullUp);
					}else{
						refresh(pullUp);
					};
					setTimeout(function(){
						pullUp.style.position = 'absolute';
   						pullUp.style.top = '-40px';
   						isUp = false;
   						swiper.update();
					},300);
   				},1000);
			};
			
			if(isDown){
				var pullDown = swiper.slides.eq(0).find('.pull-down')[0];
				pullDown.style.marginBottom = '0';
				pullDown.innerHTML = '正在加载…';
				swiper.update();
				setTimeout(function(){
					if(typeof options.down == 'function'){
						options.down(pullDown);
					}else{
						reload(pullDown);
					};
	   				setTimeout(function(){
	   					isDown = false;
   						pullDown.style.marginBottom = '-40px';
	   					swiper.update();
	   				},300);
   				},1000);
			};
			
		},
	});
	return swiper;
};

//禁用上下拉动
function useSwiper(Node){
	var swiper = new Swiper(Node,{
		direction: 'vertical',//vertical：竖向切换,horizontal: 横向切换
	    loop: false,
	    slidesPerView: 'auto',
	    setWrapperSize :true,
	    mousewheel: true,
	    
	    resistanceRatio: 0,

	    freeMode:true,
	    passiveListeners : false,
	    
	    preloadImages:false,//默认为true，Swiper会强制加载所有图片。
		updateOnImagesReady : true,//当所有的内嵌图像（img标签）加载完成后Swiper会重新初始化
		
	});
};


//创建banner
function createBanner(obj,options){
	
	var options = options == undefined ? options = {} : options = options;
	options.loop == undefined ? options.loop = true : options.loop ;
	options.direct == undefined ? options.direct = 'horizontal' : options.direct ;
	
	var swiper = new Swiper(obj,{
		loop: options.loop,//循环
		autoplay:{
			disableOnInteraction: false,//切换后还会继续自动轮播
		},
		watchOverflow:true,//slide为1个时,swiper会失效且隐藏导航等。
		direction:options.direct,
		
		preloadImages:false,//默认为true，Swiper会强制加载所有图片。
		updateOnImagesReady : true,//当所有的内嵌图像（img标签）加载完成后Swiper会重新初始化
		
		pagination: {
	      el: obj+' .swiper-pagination',
	    },
	});
	return swiper;
};


//等比例控制图片
function imgRate(){
	//获取全部图片
	var target = $('body').find('*[data-rate]');
	target.each(function(){
		var node = $(this);
		var nodeWidth = node.width();
		var rate = node.attr('data-rate');
		var reg = /\//g;
		if(rate){
			if(reg.test(rate)){
				//判断是否为分数
				var arr = rate.split("/");
				rate = parseInt(arr[0])/parseInt(arr[1]);
			};
			node.height(nodeWidth/rate);
		};
	});
};

$(window).resize(function(){
	imgRate();
});


/* 获取url中的参数
 * name: 参数名
 */
function getUrlParam(name){
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg);  //匹配目标参数
	if (r != null) return decodeURI(r[2]); return null; //返回参数值
};


/* 复制数据原型
 * obj  string 数据所在的容器对象的class或id
 * size int    复制的数据数量
 * */
function copy(obj,size){
	this.obj=$(obj);
	this.list=$(obj).html();
	this.size=size;
};
copy.prototype.data=function(){
	for(var i=0;i<this.size;i++){
		this.obj.append(this.list);
	};
};




//iframe方式打开子页面
//function openSubMain(url,title=null){
//	$(".subMain").css("transform",'translate3d(0,0,0)');
//	if( title!=null && $(".subMain").find('.header').length>0 ){
//		$(".subMain").find('.header .title').text(title);
//	};
//	var res=window.open(url,'myFrame');
//	//获取子页面
//	if(res){
//		var iframe = $("#myFrame").contents();
//		return iframe;
//	};
//};

//打开子页面
function openSubMain(obj){
	$(obj).css("transform",'translate3d(0,0,0)');
};

